<script setup>
import { ref, watch } from 'vue'
import { User, Lock } from '@element-plus/icons-vue'
import { useUserStore } from '@/stores'
import { useRouter } from 'vue-router'
import { userGetInfoService, userLoginService } from '@/api/user'
//登录表单
const formModel = ref({
    username: '',
    password: '',
    repassword: ''
})

const form = ref()

const isRegister = ref(false)

//表单验证规则
const rules = {
    username: [
        { requeired: true, message: '请输入用户名', trigger: 'blur' },
        { min: 6, max: 32, message: '用户名必须是6-32为字符', trigger: 'blur' }
    ],
    password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        {
            pattern: /^\S{6,15}$/,
            message: '密码必须是6-32的非空字符',
            trigger: 'blur'
        }
    ],
    repassword: [
        { required: true, message: '请再次输入密码', trigger: 'blur' },
        { pattern: /^\S{6,32}$/, message: '密码必须是 6-32位 的非空字符', trigger: 'blur' },
        {
            validator: (rule, value, callback) => {
                if (value != formModel.value.password) {
                    callback(new Error('两次输入密码不一致'))
                } else {
                    callback() //校验成功
                }
            },
            trigger: 'blur'
        }
    ]
}

//注册账号
const register = async () => {
    //等待验证成功
    await form.value.validate()
    //注册
}

//登录账号
const userStore = useUserStore()
const router = useRouter()
const login = async () => {
    await form.value.validate()
    const res = await userLoginService(formModel.value)
    userStore.setToken(res.data.token)
    ElMessage.success('登陆成功')
    router.push('/')
}

//切换登录注册时，重置表单内容
watch(isRegister, () => {
    formModel.value = {
        username: '',
        password: '',
        repassword: ''
    }
})
</script>

<template>
    <el-row class="login-page">
        <el-col :span="12" class="bg"></el-col>
        <el-col :span="6" :offset="3" class="form">
            <!-- 注册表单 -->
            <el-form :model="formModel" :rules="rules" ref="form" size="large" autocomplete="off" v-if="isRegister">
                <el-form-item>
                    <h1>注册</h1>
                </el-form-item>
                <el-form-item prop="username">
                    <el-input v-model="formModel.username" placeholder="请输入用户名" :prefix-icon="User"> </el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input v-model="formModel.password" type="password" placeholder="请输入密码" :prefix-icon="Lock"></el-input>
                </el-form-item>
                <el-form-item prop="repassword">
                    <el-input v-model="formModel.repassword" type="password" placeholder="请再次输入密码" :prefix-icon="Lock"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button @click="register" class="button" type="primary" auto-insert-space>注册</el-button>
                </el-form-item>
                <el-form-item class="flex">
                    <el-link type="info" :underline="false" @click="isRegister = false">已有账户，登录</el-link>
                </el-form-item>
            </el-form>
            <!-- 登录表单 -->
            <el-form v-if="!isRegister" :model="formModel" :rules="rules" size="large" autocomplete="off" ref="form">
                <el-form-item>
                    <h1>登录</h1>
                </el-form-item>
                <el-form-item prop="username">
                    <el-input v-model="formModel.username" placeholder="请输入用户名" :prefix-icon="User"></el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input @keyup.enter.native="login" v-model="formModel.password" placeholder="请输入密码" type="password" :prefix-icon="Lock"></el-input>
                </el-form-item>
                <!-- <el-form-item class="flex">
                    <div class="flex">
                        <el-checkbox>记住我</el-checkbox>
                        <el-link type="primary" :underline="false">忘记密码?</el-link>
                    </div>
                </el-form-item> -->
                <el-form-item class="flex">
                    <el-button type="primary" class="button" @click="login" auto-insert-space>登录</el-button>
                </el-form-item>
                <el-form-item class="flex">
                    <el-link type="info" :underline="false" @click="isRegister = true">没有帐户，注册</el-link>
                </el-form-item>
            </el-form>
        </el-col>
    </el-row>
</template>

<style lang="scss" scoped>
.login-page {
    height: 100vh;
    background-color: #fff;
    .bg {
        background: url('@/assets/login_bg.jpg') no-repeat center / cover;
        border-radius: 0 20px 20px 0;
    }
    .form {
        display: flex;
        flex-direction: column;
        justify-content: center;
        .button {
            width: 100%;
        }
        .flex {
            width: 100%;
            display: flex;
            justify-content: space-between;
        }
    }
}
</style>
